package com.woniuxy.core.util;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期工具类
 */
@Component
public class DateUtil {

    /**
     * 字符串转localDateTime
     * @param dateStr 日期字符串
     * @return localDateTime
     */
    public static LocalDateTime coverLocalDate(String dateStr){
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(dateStr,sdf);
    }

    /**
     * 获取日期字符串时间戳
     * @param dateStr 日期字符串
     * @return 时间戳
     * @throws ParseException
     */
    public static long coverTime(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date= sdf.parse(dateStr);
        long timestamp = date.getTime();
        return timestamp;
    }

    /**
     * 比较两个日期字符串的大小
     * @param d1 日期字符串1
     * @param d2 日期字符串2
     * @return 布尔值
     * @throws ParseException
     */
    public static boolean compareDate(String d1,String d2) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse(d1);
        long timestamp1 = date1.getTime();
        Date date2 = sdf.parse(d2);
        long timestamp2 = date2.getTime();
        if (timestamp1>timestamp2){
            return true;
        }
        return false;
    }

    /**
     * 时间错转日期字符串
     * @param plus 时间戳
     * @return 自定义格式日期字符串
     */
    public static String getTime(long plus){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.now();
        if (plus!=0){
            dateTime.plusMinutes(plus);
        }
        String formattedDateTime = dateTime.format(formatter);
        return formattedDateTime;
    }
}
